-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Kotlin] Generate open classes and methods #1815
Conversation
Hmm... I see this could clash with #1808. Maybe the |
I haven't checked this code, but you can use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great overall, but can you add back in the interface definition? I think that's still useful.
* @param noPointer Placeholder value so we can have a constructor separate from the default empty one that may be | ||
* implemented for classes extending [FFIObject]. | ||
*/ | ||
constructor(noPointer: NoPointer): super(noPointer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great docstring and I like the use of the singleton object to differentiate this from the regular constructor.
- Also make their methods open. - Make the `Pointer` in `FfiObject` optional. - Add a `constructor(noPointer: NoPointer)` to create a FFiObject with no pointer for fakes. - Remove redundant interfaces since they can be implemented, but not used in any way. - Add tests for fakes.
81e622a
to
fafcc3e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for the contribution!
This PR is an alternative implementation of #1782 , with the 1st approach @bendk proposed here:
The implementation changes are:
FFIObject.pointer
is now nullable. I know it's not great, but it's necessary as far as I can tell and I haven't seen any issues with it so far.FFIObject
now has an alternateconstructor(noPointer: NoPointer)
. I tried using an empty constructor or one withpointer: Pointer?
as the only parameter, but it clashed with the code in generated classes.FFIObject.freeRustArcPtr
implementation in the different classes now does a null check before trying to free the pointer, so you don't need to override it in the fakes, and methods usingcallWithPointer
will unwrap the nullable value and throw aNullPointerException
if it doesn't exist.FooInterface
and just pass it to afn bar(foo: Foo)
function without aClassCastException
, and the open class can now act as a kind of interface (again, I might be wrong about this, they could be restored). I did keep the interface for the trait interfaces and their impls.I added a few tests displaying how these open classes can be extended, used and not used in a real project.
I feel like this approach isn't as correct as the one in #1782 where you'd be forced to provide an alternative implementation for the interfaces that doesn't crash, but it's way simpler to maintain and the drawbacks I see as a lib user aren't that bad.